package com.hot100;

public class L0019 {
    public static void main(String[] args) {
        ListNode listNode = new ListNode(1);
        listNode.next = new ListNode(2);
        ListNode listNode1 = removeNthFromEnd(listNode, 2);
        while (listNode1!=null){
            System.out.println(listNode1);
            listNode1 =listNode1.next;
        }
    }
    static int  count = 0;
    public static ListNode removeNthFromEnd(ListNode head, int n) {
        if (head == null || head.next == null) return null;
        ListNode per = new ListNode(-1);
        per.next = head;
        recur(per,n);
        return per.next;
    }

    public static void recur(ListNode node, int n){
        if (node != null)
            recur(node.next,n);
        if (count-1 == n){
            if (node.next.next == null){
                node.next = null;
            }else {
                node.next=node.next.next;
            }
        }
        count++;
    }
}

  class ListNode{
     int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }

      @Override
      public String toString() {
          return "ListNode{" +
                  "val=" + val +
                  '}';
      }
  }